/* GSManufactureWriteAction.java
 *
 * Project/Product: APEP-JZCG
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   peng.ning	2008-10-28	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductTemporaryBean;
import com.hitrust.gs.bean.GSProductTemporaryMaintainBean;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.form.GSProductTemporaryMaintainForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSProductTemporaryMaintainView;


public class GSProductTemporaryMaintainWriteAction extends GSBaseAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.apep.jzcg.service.GSBaseAction#execute(com.apep.jzcg.service.GSBaseForm
	 * )
	 */
	public String execute(GSBaseForm aForm) throws Exception {

		GSProductTemporaryMaintainForm form = (GSProductTemporaryMaintainForm) aForm;
		LoginContext context = (LoginContext) form.getSession().getAttribute(
				GSBaseConstants.LOGIN);
		String[] index = form.getSel_manufacture();
		String[] rid = form.getGs_recorder_id();
		ResultProcess rst = (ResultProcess) aForm.getSession().getAttribute(
				"productTemporaryMaintainExcel");
		ArrayList array = rst.getAllRecoreds();
		boolean bl = false;
		try {	
			for (int a = 0; a < index.length; a++) {
				int i = 0;
				for (int tJ = 0; tJ < rid.length; tJ++) {
					if (index[a].equals(rid[tJ])) {
						i = tJ;
						break;
					}
				}
	
				String temporaryConcentricityCode = form.getGs_temporary_concentricity_code()[i].trim();
				String recordid= form.getGs_recorder_id()[i].trim();
				int xh=Integer.parseInt(recordid)+1;
				
				GSManufactureDAO dao = new GSManufactureDAO();
				
				GSProductTemporaryMaintainBean pubean = dao.getProductTemporaryMaintainByTemporaryConcentricityCode(
						temporaryConcentricityCode," select * from t_product_temporary_maintain ");
				
				if(pubean!=null){
					if("0".equals(pubean.getGs_auditing_status())||"1".equals(pubean.getGs_auditing_status())){
						throw new GSException(
								"序号"+ xh + ": "
								+ "被替换的14位临时产品已经存在相同记录!");
					}
				}
				
				GSProductTemporaryBean tempbean = dao.getProductTemporaryByTemporaryConcentricityCode(
						temporaryConcentricityCode," select * from t_product_temporary ");
				if(tempbean!=null){	
						throw new GSException(
							"序号"+ xh + ": "
							+ "被替换的14位临时产品已经被替换过一次!");
				}

			}
			
				for (int a = 0; a < index.length; a++) {
					int i = 0;
					for (int tJ = 0; tJ < rid.length; tJ++) {
						if (index[a].equals(rid[tJ])) {
							i = tJ;
							break;
						}
					}

				
					
					String concentricityCode = form.getGs_concentricity_code()[i].trim();
					String temporaryConcentricityCode = form.getGs_temporary_concentricity_code()[i].trim();
					
					String enabledtime= form.getGs_enabled_time()[i].trim();
					
					GSManufactureDAO dao = new GSManufactureDAO();
					GSManufactureBean mbean = dao.getManufactureByConcentricityCode(
							concentricityCode, " select * from t_product ");
					GSManufactureBean tempbean = dao.getManufactureByConcentricityCode(
							temporaryConcentricityCode, " select * from t_product ");
					
					GSProductTemporaryMaintainBean pbean=new GSProductTemporaryMaintainBean();
					pbean.setGs_concentricity_code(concentricityCode);
					pbean.setGs_product_id(mbean.getId());
					pbean.setGs_temporary_concentricity_code(temporaryConcentricityCode);
					pbean.setGs_temporary_product_id(tempbean.getId());
					pbean.setGs_create_user_id(context.getLoginUserId());
					pbean.setGs_create_user(context.getLoginUserName());
					pbean.setGs_create_time(new framework.core.DateUtil().getFullString());
					pbean.setGs_enabled_time(enabledtime);
					pbean.setGs_auditing_status("0");
					pbean.setGs_status("0");
					bl=dao.insertProductTemporaryMaintain(pbean);
					
					if (bl) {
						// 移除
						int row = array.size();
						for (int j = 0; j < row; j++) {
							GSProductTemporaryMaintainView mv = (GSProductTemporaryMaintainView) array
									.get(j);
							if (index[a].equals(mv.getGs_recorder_id())) {
								array.remove(mv);
								break;
							}
						}
					}
				}
				
				if (array != null && array.size() == 0) {
					aForm.getSession().removeAttribute("productTemporaryMaintainExcel");
					aForm.getRequest().setAttribute("message",
							"产品确认写入成功，无可确认产品信息，返回继续添加。");
					aForm.getRequest().setAttribute("action", "A190015.do");
				} else if (array != null && array.size() > 0) {
					aForm.getRequest().setAttribute("message", "产品确认写入成功");
					if (aForm.getRequest().getParameter("choice").equals("")) {
					aForm.getRequest().setAttribute("action","A190017.do");
					}
				}
		} catch (GSException e) {
			GSLogWriter.error(this, e.toString());
			aForm.getRequest().setAttribute("action", "A190017.do");
			aForm.getRequest().setAttribute("target", "_self");
			throw new GSException(e.getMessage());
		} finally {
			rst.setResult(array);
			aForm.getSession().setAttribute("productTemporaryMaintainExcel", rst);
		}
		return GSBaseConstants.FORWARD_SUCCESS;
	}
}
